
<template>
  <el-form ref="deptForm" :model="form" label-width="120px" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in employees" :key="item.id" :label="item.username" :value="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getemployees, addemployees } from '@/api/employees'
import { getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  // 子组件接收父组件传递过来的 id
  props: {
    id: {
      type: String,
      required: true
    },
    // 子组件接收父组件传递过来的状态
    isEdit: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      employees: []
    }
  },
  created() {
    this.getEmployees()
    this.getDepartDetail()
  },
  methods: {
    // 部门详情
    async getDepartDetail() {
      // 趁早返回
      if (!this.isEdit) return
      const res = await getDepartDetail(this.id)
      //   console.log(res)
      // 完成数据回填
      this.form = res.data
    },
    // 员工的基本信息
    async getEmployees() {
      const res = await getemployees()
      //   console.log(res)
      this.employees = res.data
      // 循坏展示列表
    },
    // 添加
    async todoAdd() {
      // 开始兜底校验
      // 发送请求,完成添加操作
      this.form.pid = this.id
      // 携带参数,添加成功
      const res = await addemployees(this.form)
      //   console.log(res)
      // 提醒用户,关闭弹层,重新渲染
      this.$message.success(res.message)
      this.$emit('success')
    },
    // 编辑
    async todoEdit() {
      // 开始兜底校验
      // 发送请求,完成添加操作
      // 携带参数,添加成功
      const res = await updateDepartments(this.form)
      //   console.log(res)
      // 提醒用户,关闭弹层,重新渲染
      this.$message.success(res.message)
      this.$emit('success')
    },
    // 点击确定,
    hSubmit() {
      this.$refs.deptForm.validate(valid => {
        console.log(valid)
        // 兜底校验不通过的话,终止
        if (!valid) return
        // 判断是编辑还是添加
        this.isEdit ? this.todoEdit() : this.todoAdd()
      })
    },
    // 点击取消,关闭弹框
    hCancel() {
      this.$emit('close')
    }
  }
}
</script>
<style scoped>

</style>
